var Push = {
    isDebug: false,
    clearTag: function(){
        try {
            uexJPush.setTag('xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx');
        }catch(e) {
             if(Push.isDebug) alert('clear alias e: ' + e);
        }
        
    },
    setTag:function(name){
        try {
            uexJPush.setTag(name);
        }catch(e) {
             if(Push.isDebug) alert('clear alias e: ' + e);
        }        
    },
    isAppOne: false,    /**app是否激活:跳转页面用(android,ios)*/
    isReceiveMsg: false,    /**app是否收到信息(ios)*/
    isResume: false,    /**app是否恢复(ios)*/
    timerLive: 50,  /**监听器最长存活时间(单位：100ms,本处设置50*100=5s)*/
    startPushIOS: function(){
        try{
            uexDevice.cbGetInfo = function(opId,dataType, data){
                var device = eval('('+data+')');
                if(Push.isDebug){
                    var t = JSON.stringify(device);
                    alert('cbGetInfo device: ' + t);   
                }
                /**获取deviceToken*/
                var deviceToken = device.deviceToken;
                if(deviceToken){
                    if(Push.isDebug) alert("devictToken 111="+deviceToken);
                    uexJPush.setDeviceToken(deviceToken);
                }
            }
            /**设置是否接受推送*/
            uexJPush.setPushStatus(1);
            uexDevice.getInfo('11');
        }catch(e){
            if(Push.isDebug) $alert('Push startPushIOS exits error : ' + e);
        }
    },
    setPush:function(a){
        try{
            uexJPush.setPushStatus(a);
            uexJPush.getPushStatus();
        }catch(e){
            if(Push.isDebug) $alert('Push setPush(ios) exits error: ' + e);
        }
    },
    init: function(){
        
        if(IsAndroid){
            Push.initAndroid();
        }else if(IsIPhone){
            Push.initIOS();
        }
    },
    initIOS: function(){
        try{
            OpenCount = 0;
            setTimeout(function(){
                window['myRootName'] = 'hello';
            },5000);
            
            uexJPush.cbGetPushStatus=function(opId,dataType,data){
                if(Push.isDebug) alert("push cbGetPushStatus ="+data);
            }
            uexJPush.cbSetTag= function(opId,dataType,data){
                if(Push.isDebug) alert("jpush cbSetTag="+data);
            }
            uexJPush.cbGetPushInfo=function(opId,dataType,data){
                try{
                    Push.isReceiveMsg = true;
                    if(Push.isDebug){
                        alert("推送消息格式为："+data);
                    }
                    /**打开的次数、是否打开过*/
                   if(window['myRootName']){
                       if(Push.isDebug) alert('app 启动');
                       var msgTimerCount = 0;
                       var msgTimer = setInterval(function(){
                           msgTimerCount++;
                           if(msgTimerCount == Push.timerLive){
                               clearInterval(msgTimer);
                           }
                           if(Push.isResume){
                               clearInterval(msgTimer);
                               Push.isAppOne = true;
                               Push.toChat(data);
                               Push.isResume = false;
                           }
                       },100);
                   }else{
                       if(Push.isDebug) alert('app 未启动');
                       Push.isAppOne = true;
                       Push.toChat(data);
                   }
                }catch(e){
                    alert('cbGetPushInfo exception: ' + e);
                }
                
            }
            //推送的统计
            uexWidget.onResume = function(){
                OpenCount++;
                if(OpenCount != 1){ /**非第一次启动的时候,会启用此监听器*/
                    var resumeTimerCount = 0;
                    var resumeTimer = setInterval(function(){
                        resumeTimerCount++;
                        if(resumeTimerCount == Push.timerLive){
                            clearInterval(resumeTimer);
                        }
                        if(Push.isReceiveMsg){
                            clearInterval(resumeTimer);
                            Push.isReceiveMsg = false;
                            Push.isResume = true; /**app恢复*/
                        }  
                    },100);
                }
                if(Push.isDebug) alert('i am resume! open count is : ' + OpenCount + '\tisAppOne : ' + Push.isAppOne);
            };
            Push.startPushIOS();
        }catch(e){
            if(Push.isDebug) alert('push initIOS exits error: ' + e);
        }
    },
    initAndroid: function(){
        try{
            OpenCount = 0;
            setTimeout(function(){
                window['myRootName'] = 'hello';
            },5000);
            
            uexJPush.cbGetPushInfo = function(a,b,data){
                try{
                    Push.isReceiveMsg = true;
                    if(Push.isDebug){
                        alert("推送消息格式为："+data);
                    }
                    /**打开的次数、是否打开过*/
                   if(window['myRootName']){
                       if(Push.isDebug) alert('app 启动');
                       var msgTimerCount = 0;
                       var msgTimer = setInterval(function(){
                           msgTimerCount++;
                           if(msgTimerCount == Push.timerLive){
                               clearInterval(msgTimer);
                           }
                           if(Push.isResume){
                               clearInterval(msgTimer);
                               Push.isAppOne = true;
                               Push.toChat(data);
                               Push.isResume = false;
                           }
                       },100);
                   }else{
                       if(Push.isDebug) alert('app 未启动');
                       Push.isAppOne = true;
                       Push.toChat(data);
                   }
                }catch(e){
                    alert('cbGetPushInfo exception: ' + e);
                }
            };
            uexJPush.cbSetTag = function(a,b,c){
                //返回0表示成功，其他表示失败，含义见：
                //http://docs.jpush.cn/pages/viewpage.action?pageId=557241
                //if(Push.isDebug)
                if(Push.isDebug) alert('cbSetTag='+c);
            };
             
            //推送的统计
            uexWidget.onSuspend = function(){
                //Push.isAppOne = false;
                uexJPush.onPause();
            };
            uexWidget.onResume = function(){
                uexJPush.onResume();
                OpenCount++;
                if(OpenCount != 1){ /**非第一次启动的时候,会启用此监听器*/
                    var resumeTimerCount = 0;
                    var resumeTimer = setInterval(function(){
                        resumeTimerCount++;
                        if(resumeTimerCount == Push.timerLive){
                            clearInterval(resumeTimer);
                        }
                        if(Push.isReceiveMsg){
                            clearInterval(resumeTimer);
                            Push.isReceiveMsg = false;
                            Push.isResume = true; /**app恢复*/
                        }  
                    },100);
                }
            };
            
            //uexJPush.registrationCode('你的激活码');
            uexJPush.getPushInfo();
            uexJPush.setDeviceToken('');
     
            //uexJPush.setPushStatus(0); 不接收所有推送
            //uexJPush.setPushStatus(1); 恢复接收所有推送
            /*
            uexJPush.cbGetPushStatus = function(a,b,c){
                //当前是否接收推送 1||0
                alert(c);
            };
            uexJPush.getPushStatus();
            */
        }catch(e){
            if(Push.isDebug) alert('alias init : ' + e);
        }
    },
    toChat: function(c){
        /*
        c的格式为：{"title":"标题","content":"内容","extra":"自定义数据","notifyid":12312,"apprun":1}
                        其中：notifyid为推送ID，如果需要手动清除指定的通知，可使用：
             uexJPush.clearNotificationById(notifyid)
        apprun为APP状态，若APP启动时候收到的推送，则apprun=1，否则apprun=0
        */
        try{
            var cbdata;
            if(IsAndroid){
                if(c.indexOf('\n') != -1){
                    c = c.replace('\n','');
                }
                var d = eval('('+c+')');
                cbdata = d.extra.extra;
                
            }else if(IsIPhone){
                var str = c;
                var start = '{"content":"';
                var end = '"}';
                str = str.substr(start.length,str.length);
                str = str.substr(0,str.lastIndexOf(end));
                cbdata = eval('('+str+')'); 
                
            }
             //好友聊天
            if(cbdata.type == 1){
                var chatMsg = new Object();
                chatMsg.id = cbdata.user._id;
                chatMsg.name = cbdata.user.name;
                SetLS("chat_detail",chatMsg);
                setTimeout(function(){
                    if(Push.isAppOne){
                        if(GetLS('user')){
                            var user = GetLS("user")
                            var lanPath = user.category==30?"ch/":"en/";
                            OpenNewWin(lanPath+"chat_detail");
                            Push.isAppOne = false;  
                        }
                    }
                },500);
            }
            //加好友信息
            if(cbdata.type == 2){
                setTimeout(function(){
                    if(Push.isAppOne){
                        if(GetLS('user')){
                            var user = GetLS("user")
                            var lanPath = user.category==30?"ch/":"en/";
                            OpenNewWin(lanPath+"chat");
                            Push.isAppOne = false;  
                        }
                    }
                },500);
            }
            //群聊
            if(cbdata.type == 3){
                setTimeout(function(){
                    if(Push.isAppOne){
                        if(GetLS('user')){
                            var user = GetLS("user")
                            var lanPath = user.category==30?"ch/":"en/";
                            OpenNewWin(lanPath+"grouplist");
                            Push.isAppOne = false;
                        }
                    }
                },500);
            }
           
        }catch(e){
            if(Push.isDebug) alert('me err: ' + e);
        }
    }
}
